package com.bjsxt.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.DateFormatUtil;
import com.bjsxt.common.util.FileUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.system.dao.IBankDao;
import com.bjsxt.system.dao.ICateDao;
import com.bjsxt.system.dao.IGrantDao;
import com.bjsxt.system.dao.IInverstorDao;
import com.bjsxt.system.dao.IRegionDao;
import com.bjsxt.system.pojo.ABank;
import com.bjsxt.system.pojo.ACate;
import com.bjsxt.system.pojo.AGrant;
import com.bjsxt.system.pojo.AInverstor;
import com.bjsxt.system.pojo.Region;
import com.bjsxt.system.service.IAreaService;
import com.bjsxt.user.pojo.AAdmin;
@Service("areaService")
public class IAreaServiceImpl implements IAreaService
{
	@Resource
	private IRegionDao regionDao;
	@Resource
	private ICateDao cateDao;
	@Resource
	private IBankDao bankDao;
	@Resource
	private IInverstorDao inverstorDao;
	@Resource
	private DateFormatUtil dateFormatUtil;
	@Resource
	private FileUtil fileUtil;
	@Resource
	private IGrantDao grantDao;
	@Override
	public Region findOneRegion(HashMap<String, Object> condMap)
	{
		Region region = regionDao.findOne(condMap);
		return region;
	}

	@Override
	public List<Region> findListRegion(PageInfoUtil pageInfoUtil,HashMap<String, Object> condMap)
	{
		List<Region> list=null;
		if(pageInfoUtil != null)
		{	
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			condMap.put("countTotal", "");
			List<Region> adminList = regionDao.findList(condMap);
			int count = adminList.get(0).getId();
			pageInfoUtil.setTotalRecord(count);
			condMap.remove("countTotal");
			condMap.put("pageInfo", "");
			condMap.put("curRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			list= regionDao.findList(condMap);
			return list;
		}else
		{
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			 list = regionDao.findList(condMap);
		}
		return list;
	}

	@Override
	public ACate findOneACate(HashMap<String, Object> condMap)
	{
		
		return  cateDao.findOne(condMap);
	}

	@Override
	public List<ACate> findListACate(PageInfoUtil pageInfoUtil, HashMap<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ABank findOneABank(HashMap<String, Object> condMap)
	{
			
		
		return bankDao.findOne(condMap);
	}

	@Override
	public List<ABank> findListABank(PageInfoUtil pageInfoUtil, HashMap<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AInverstor findOneAInverstor(HashMap<String, Object> condMap)
	{
		
		return inverstorDao.findOne(condMap);
	}

	@Override
	public List<AInverstor> findListAInverstor(PageInfoUtil pageInfoUtil, HashMap<String, Object> condMap)
	{
		List<AInverstor> list=null;
		if(pageInfoUtil != null)
		{	
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			condMap.put("countTotal", "");
			List<AInverstor> adminList = inverstorDao.findList(condMap);
			int count = adminList.get(0).getId();
			pageInfoUtil.setTotalRecord(count);
			condMap.remove("countTotal");
			condMap.put("pageInfo", "");
			condMap.put("curRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			list= inverstorDao.findList(condMap);
			return list;
		}else
		{
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			 list = inverstorDao.findList(condMap);
		}
		return list;
	}

	@Override
	public JSONObject updateAInverstor(AInverstor inverstor,Map condMap) throws IOException
	{	
		JSONObject json = new JSONObject();
		int rs = inverstorDao.update(inverstor);
		
		if(rs>0)
		{
			json.put("info", "更新成功");
			json.put("code", 1);
			MultipartFile file =(MultipartFile) condMap.get("file");
			/* 将文件以前的路径先保存下来*/
			String oldPath=""+condMap.get("pubPath")+inverstor.getImg();
			/* 得到公共路径 */
			String realPath=""+condMap.get("pubPath");
			/* 拼接本地的真实路径 */
			/* 真实路径是由自定义文件夹/date/uuid.文件的后缀名 */
			if(file!=null && !"".equals(file.getOriginalFilename()))
			{
				String truePath ="upload/inverstor/"+this.dateFormatUtil.format(new Date(), "yyyy-MM-dd");
				truePath=truePath+"/"+UUID.randomUUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
				realPath=realPath+truePath;
				System.out.println(realPath);
				/* 进行文件复制 */
				File targetFile = new File(realPath);
				if(this.fileUtil.copy(file.getInputStream(), targetFile))
				{
					/*图片copy成功之后就给数据库更新path*/
					inverstor.setImg(truePath);
					inverstorDao.update(inverstor);
				}
				/* 删除老照片 */
				File oldFile = new File(oldPath);
				if(oldFile.exists())
				{
					boolean flag = oldFile.delete();
					if(flag)
					{
						System.out.println("------图片删除失败了---------");
					}
				}
			}
		}else
		{
			json.put("info", "更新失败");
			json.put("code", 0);
		}
 		return json;
	}

	@Override
	public AGrant findOneAGrant(HashMap<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AGrant> findListAGrant(PageInfoUtil pageInfoUtil, HashMap<String, Object> condMap)
	{
		List<AGrant> list=null;
		if(pageInfoUtil != null)
		{	
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			condMap.put("countTotal", "");
			List<AGrant> adminList = grantDao.findList(condMap);
			int count = adminList.get(0).getId();
			pageInfoUtil.setTotalRecord(count);
			condMap.remove("countTotal");
			condMap.put("pageInfo", "");
			condMap.put("curRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			list= grantDao.findList(condMap);
			return list;
		}else
		{
			String keyword =(String)condMap.get("keyword");
			if(keyword !=null){
				condMap.put("keyword", "%"+keyword+"%");
			}
			 list = grantDao.findList(condMap);
		}
		return list;
	}
	

}
